package com.lancoo.hanvon;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import com.hanvon.HWCloudManager;

import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Tool {
    @SuppressLint("MissingPermission")
    public static String generateIDCompat(Context context) {
        TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        // SDK中获取m_szImei的这行代码会崩溃，需要适配版本
        String m_szImei;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                m_szImei = TelephonyMgr.getImei();
            } else {
                m_szImei = TelephonyMgr.getDeviceId();
            }
        } catch (Exception e) {
            e.printStackTrace();
            m_szImei = "god";
        }
        String m_szDevIDShort = "35" + Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10 + Build.TYPE.length() % 10 + Build.USER.length() % 10;
        String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), "android_id");
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
        BluetoothAdapter m_BluetoothAdapter = null;
        if (Build.VERSION.SDK_INT <= 17) {
            m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        } else if (Build.VERSION.SDK_INT >= 18) {
            BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
            m_BluetoothAdapter = bluetoothManager.getAdapter();
        }

        String m_szBTMAC = "";
        if (m_BluetoothAdapter != null) {
            m_szBTMAC = m_BluetoothAdapter.getAddress();
        }

        String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID + m_szWLANMAC + m_szBTMAC;
        MessageDigest m = null;

        try {
            m = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var15) {
            var15.printStackTrace();
        }

        m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
        byte[] p_md5Data = m.digest();
        String m_szUniqueID = new String();

        for (int i = 0; i < p_md5Data.length; ++i) {
            int b = 255 & p_md5Data[i];
            if (b <= 15) {
                m_szUniqueID = m_szUniqueID + "0";
            }

            m_szUniqueID = m_szUniqueID + Integer.toHexString(b);
        }

        m_szUniqueID = m_szUniqueID.toUpperCase();
        return m_szUniqueID;
    }

    public static String findFieldValueOfHWCloudManager(HWCloudManager obj, String fieldName) {
        Class<?> clazz = HWCloudManager.class;
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            Object o = field.get(obj);
            if (o != null) return o.toString();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return "";
    }
}